﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Polaris.Common.FilterAttribute;
using Polaris.Domain.Interfaces.Admin;
using Polaris.Domain.Model.Dto.Article.Request;

namespace Polaris.WebApi.Controllers.Admin
{
    /// <summary>
    /// 文章控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ArticleController : BaseController
    {

        private readonly IArticle _article;

        public ArticleController(IArticle article)
        {
            _article = article;
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        [HttpPost]
        //[UnitOfWorkFilter(IsTransactional = true)]
        public async Task<IActionResult> CreateArticle(CreateArticleInfoRequest create)
        {
            await _article.CreateArticle(create, null);
            return SUCCESS("操作成功");
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        [HttpPost]
        //[UnitOfWorkFilter(IsTransactional = true)]
        public async Task<IActionResult> UpdateArticle(UpdateArticleInfoRequest update)
        {
            await _article.UpdateArticle(update, null);
            return SUCCESS("操作成功");
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        //[UnitOfWorkFilter(IsTransactional = true)]
        public async Task<IActionResult> GetArticleList([FromQuery] ArticleQueryRequest query)
        {
            var result = await _article.GetArticleList(query);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id获取文章预览内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleContentById(string id)
        {
            var result = await _article.GetArticleContentById(id);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id获取文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleInfoById(string id)
        {
            var result = await _article.GetArticleInfoById(id);
            return SUCCESS(result);
        }

        /// <summary>
        /// 删除文章 根据id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteArticleById(string id)
        {
            await _article.DeleteArticleById(id, null);
            return SUCCESS("操作成功");
        }
    }
}
